

import time
import logging
import op.op_http as op_http

# 任务 2 : 调度评估

# curl -X POST http://10.244.0.66:5001/api/task/create -H "Content-Type: application/json" -d '{"requirements":{"cpu": {"cpu_usage": 31.1, "cpu_count": 32, "cpu_freq": 2384.36153125}, "memory": {"memory_total": 134659764224, "memory_available": 113606070272, "memory_used": 19749892096, "memory_percent": 15.6}, "disk": {"disk_total": 1947177910272, "disk_free": 1651186610176, "disk_used": 197004574720, "disk_percent": 10.7}, "gpu": {"gpu_memory_free":12415923815}, "links": [{"sat_id": 2, "time": 5}, {"sat_id": 4, "time": 5}, {"sat_id": 20, "time": 5}, {"sat_id": 22, "time": 5}]}, "sliceCount": 5}'

# curl -X POST http://10.244.0.66:5001/api/task/create 
#       -H "Content-Type: application/json" 
#       -d '{"requirements":{"cpu": {"cpu_usage": 31.1, "cpu_count": 32, "cpu_freq": 2384.36153125}, "memory": {"memory_total": 134659764224, "memory_available": 113606070272, "memory_used": 19749892096, "memory_percent": 15.6}, "disk": {"disk_total": 1947177910272, "disk_free": 1651186610176, "disk_used": 197004574720, "disk_percent": 10.7}, "gpu": {"gpu_memory_free":12415923815}, "links": [{"sat_id": 2, "time": 5}, {"sat_id": 4, "time": 5}, {"sat_id": 20, "time": 5}, {"sat_id": 22, "time": 5}]}, "sliceCount": 5}'


class Task2_config:
    def __init__(self):
        self.base_url = "http://10.244.0.66:5001"
        self.endpoint = "/api/task/create"
        self.method = "POST"
        self.headers = {"Content-Type": "application/json"}
        self.data = {
            "requirements": {
                "cpu": {
                    "cpu_usage": 31.1,
                    "cpu_count": 32,
                    "cpu_freq": 2384.36153125
                },
                "memory": {
                    "memory_total": 134659764224,
                    "memory_available": 113606070272,
                    "memory_used": 19749892096,
                    "memory_percent": 15.6
                },
                "disk": {
                    "disk_total": 1947177910272,
                    "disk_free": 1651186610176,
                    "disk_used": 197004574720,
                    "disk_percent": 10.7
                },
                "gpu": {
                    "gpu_memory_free": 12415923815
                },
                "links": [
                    {"sat_id": 2, "time": 5},
                    {"sat_id": 4, "time": 5},
                    {"sat_id": 20, "time": 5},
                    {"sat_id": 22, "time": 5}
                ]
            },
            "sliceCount": 5
        }

        # self.curl_param = '''-X POST http://10.244.0.66:5001/api/task/create -H "Content-Type: application/json" -d '{"requirements":{"cpu": {"cpu_usage": 31.1, "cpu_count": 32, "cpu_freq": 2384.36153125}, "memory": {"memory_total": 134659764224, "memory_available": 113606070272, "memory_used": 19749892096, "memory_percent": 15.6}, "disk": {"disk_total": 1947177910272, "disk_free": 1651186610176, "disk_used": 197004574720, "disk_percent": 10.7}, "gpu": {"gpu_memory_free":12415923815}, "links": [{"sat_id": 2, "time": 5}, {"sat_id": 4, "time": 5}, {"sat_id": 20, "time": 5}, {"sat_id": 22, "time": 5}]}, "sliceCount": 5}' '''
        # 调用函数生成 curl 命令

        self.curl_param = op_http.generate_curl_command(self.base_url, self.endpoint, self.method, self.headers, self.data)

        # 输出生成的命令
        print("config.curl_param:", self.curl_param)

class Task2_info:
    def __init__(self):
        self.calculateSallite = None

task2_config = Task2_config()
task2_info = Task2_info()

def run():
    logging.info(">>>>")
    logging.info("Scene-2 task2 Run...")
    logging.info("<<<<")

    task2_info.calculateSallite = work1_call_researchGroup2_by_scheduler()

    while(end_check() == False):
        time.sleep(1)
        pass

    return True


def res():
    return task2_info.calculateSallite

def work1_call_researchGroup2_by_scheduler():
    logging.info(">>>>")
    logging.info("work1 call researchGroup2 by scheduler...")
    list_resourceConsumptionAssessment_CPU_GPU = {}

    respons = op_http.curl_command(task2_config.curl_param)
    print(respons)

    calculateSatellite = respons['calculateSatellite']

    # call api
    logging.info("<<<<")

    return calculateSatellite

#检测当前任务是否执行完成, 满足完成条件时，返回True
def end_check():
    logging.info(">>>>")
    logging.info("Scene-2 task3 end check...")
    logging.info("<<<<")
    return True